
#include "character.h"
#include<QTimer>
#include<QKeyEvent>
#include<QUrl>
#include<QLabel>

Character::Character()
{
    QPixmap pix("://resource/standing.png");//加载初始图片并设置属性
    QGraphicsPixmapItem* poseStanding=new QGraphicsPixmapItem(pix);
    this->pose=poseStanding;
    this->pose->setScale(0.3);
    this->pose->setPos(0,460);
    this->pose->setFlag(QGraphicsItem::ItemIsMovable, false);
    this->width=pix.width()*0.3;
    this->height=pix.height()*0.3;

    this->attackTimer=new QTimer;

    this->damage=200;
    this->health=10000;

    this->attackoutput=new QAudioOutput(this);//加载攻击音效
    this->attack1=new QMediaPlayer(this);
    this->attack1->setSource(QUrl("qrc:/resource/attack1.mp3"));
    this->attack1->setAudioOutput(attackoutput);
    this->attack1->setLoops(1);

    this->attackoutput=new QAudioOutput(this);
    this->attack2=new QMediaPlayer(this);
    this->attack2->setSource(QUrl("qrc:/resource/attack2.mp3"));
    this->attack2->setAudioOutput(attackoutput);
    this->attack2->setLoops(1);

    this->attackoutput=new QAudioOutput(this);
    this->attack3=new QMediaPlayer(this);
    this->attack3->setSource(QUrl("qrc:/resource/attack3.mp3"));
    this->attack3->setAudioOutput(attackoutput);
    this->attack3->setLoops(1);

    this->attackoutput=new QAudioOutput(this);
    this->attack4=new QMediaPlayer(this);
    this->attack4->setSource(QUrl("qrc:/resource/attack4.mp3"));
    this->attack4->setAudioOutput(attackoutput);
    this->attack4->setLoops(1);

    this->attackoutput=new QAudioOutput(this);
    this->attack5=new QMediaPlayer(this);
    this->attack5->setSource(QUrl("qrc:/resource/attack5.mp3"));
    this->attack5->setAudioOutput(attackoutput);
    this->attack5->setLoops(1);

    this->attackoutput=new QAudioOutput(this);//加载受击音效
    this->beattack=new QMediaPlayer(this);
    this->beattack->setSource(QUrl("qrc:/resource/beattack.mp3"));
    this->beattack->setAudioOutput(attackoutput);
    this->beattack->setLoops(1);

    this->attackoutput=new QAudioOutput(this);//加载失败音效
    this->fail=new QMediaPlayer(this);
    this->fail->setSource(QUrl("qrc:/resource/luminefail.mp3"));
    this->fail->setAudioOutput(attackoutput);
    this->fail->setLoops(1);

}

int Character::jumpAddition=1;
int Character::moveAddition=1;

int Character::attackflag=1;

void Character::attack()//攻击函数
{
    int tx=0;
    if(this->dest==1)
        tx=1;
    else
        tx=-1;
    QString pos=QString("://resource/attack%1.png").arg((this->attackflag));
    QPixmap temp(pos);
    if(this->dest==0)
        temp=temp.transformed(QTransform().scale(-1, 1));
    this->pose->setPixmap(temp);
    this->width=temp.width()*0.3+50;
    this->height=temp.height()*0.3;
    if(attackflag==1)
    {
        this->attack1->play();
        this->pose->setPos(this->pose->x(),this->pose->y()+24);
    }
    else if(attackflag==2)
    {
        this->attack2->play();
        this->pose->setPos(this->pose->x()+5*tx,this->pose->y()-70);
    }
    else if(attackflag==3)
    {
        this->attack3->play();
        this->pose->setPos(this->pose->x()+5*tx,this->pose->y()-65);
    }
    else if(attackflag==4)
    {
        this->attack4->play();
        this->pose->setPos(this->pose->x()+50*tx,this->pose->y()+30);
    }
    else if(attackflag==5)
    {
        this->attack5->play();
        this->pose->setPos(this->pose->x()+250*tx,this->pose->y()+30);
    }
}

void Character::beAttacked()//受击函数
{
    if(this->health>0)
        this->beattack->play();

    QString pos=QString(":/resource/standingIsAttacked.png");
    QPixmap temp(pos);
    if(this->dest==0)
        temp=temp.transformed(QTransform().scale(-1, 1));
    this->pose->setPixmap(temp);
    if(this->health<=0)
        this->fail->play();
    QTimer::singleShot(200,this,[=](){
        QPixmap pix("://resource/standing.png");
        if(this->dest==0)
            pix=pix.transformed(QTransform().scale(-1, 1));
        this->pose->setPixmap(pix);
    });
}

